package com.tdam2014.grupo3.entities;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.json.JSONException;
import org.json.JSONObject;

import com.tdam2014.grupo3.flickr.util.JSONUtils;

public class Photo {
	
	private String id;
	private String title;
	private Date datePosted;
	private int farm;
	private String secret;
	private String server;
	private int countComments;
	
	public Photo() {
	}
	
	public Photo(JSONObject photoObject) throws JSONException {
        this.id = photoObject.getString("id");
        
        if (photoObject.has("title")) {
			Object titleObject = photoObject.get("title");
			if (titleObject instanceof String) this.title = (String) titleObject;
			else this.title = JSONUtils.getChildValue(photoObject, "title");
		}
//        JSONObject titleObj = photoElement.optJSONObject("title");
//        if (titleObj != null) {
//            photo.setTitle(titleObj.getString("_content"));
//        } else {
//            photo.setTitle(photoElement.getString("title"));
//        }
        
        JSONObject datesObject = photoObject.optJSONObject("dates");
        if (datesObject != null) this.datePosted = new Date(Long.parseLong(datesObject.optString("posted")) * 1000L);
        
        this.farm = Integer.parseInt(photoObject.getString("farm"));
        this.secret = photoObject.getString("secret");
        this.server = photoObject.getString("server");
        
//        photo.setIconServer(photoElement.optString("iconserver", null));
//        photo.setIconFarm(photoElement.optString("iconfarm", null));

        if (photoObject.has("comments")) this.countComments = Integer.parseInt(JSONUtils.getChildValue(photoObject, "comments"));
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getDatePosted() {
		return datePosted;
	}

	public void setDatePosted(Date datePosted) {
		this.datePosted = datePosted;
	}

	public int getFarm() {
		return farm;
	}

	public void setFarm(int farm) {
		this.farm = farm;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public int getCountComments() {
		return countComments;
	}

	public void setCountComments(int countComments) {
		this.countComments = countComments;
	}
	
	public String getDatePostedDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		return sdf.format(this.datePosted);
	}
	
	public String getDatePostedDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return sdf.format(this.datePosted);
	}
	
	public String getDatePostedTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		return sdf.format(this.datePosted);
	}
}
